<template>
  <el-form
    :model="ruleForm"
    :rules="rules"
    ref="ruleFormRef"
    label-width="100px"
    class="demo-ruleForm"
  >
    <el-form-item label="邮箱名称" prop="email">
      <el-input
        v-model="ruleForm.email"
        placeholder="请输入邮箱账号"
      ></el-input>
    </el-form-item>
    <el-form-item label="邮箱名称" prop="pwd">
      <el-input v-model="ruleForm.pwd" placeholder="请输入邮箱密码"></el-input>
    </el-form-item>

    <el-button type="primary" @click="submitForm(ruleForm)">登陆一下</el-button>
  </el-form>
</template>

<script lang="ts">
import { defineComponent, reactive, ref } from "vue";
import { useRouter } from "vue-router";
import { useStore } from "vuex";
import type { ElForm } from "element-plus";
type FormInstance = InstanceType<typeof ElForm>;

const ruleFormRef = ref();

export default defineComponent({
  data() {
    return {
      ruleForm: {
        email: "",
        pwd: "",
      },
    };
  },
  setup() {
    const rules = reactive({
      email: [
        {
          required: true,
          message: "请输入邮箱账号",
          trigger: "blur",
        },
      ],
      pwd: [
        {
          required: true,
          message: "请输入邮箱密码",
          trigger: "blur",
        },
      ],
    });
    // 登陆
    const router = useRouter();
    const store = useStore();
    const submitForm = (ruleForm: any) => {
      ruleFormRef.value.validate((valid: Boolean) => {
        if (valid) {
          console.log("验证成功", ruleForm.email);
          // 验证成功跳到登录页

          router.push("/");
          store.commit("login");
        } else {
          console.log("验证失败");
          return false;
        }
      });
    };

    return {
      rules,
      ruleFormRef,
      submitForm,
      router,
    };
  },
});
</script>

<style scoped></style>
